package com.jlboot.app.admin.controller.sys;

import com.jlboot.core.annotation.JlActivity;
import com.jlboot.model.po.sys.SysConfig;
import com.jlboot.core.base.PageSearch;
import com.jlboot.app.admin.ui.plugins.layui.LayuiData;
import com.jlboot.core.json.JsonResult;
import com.jlboot.core.model.DataMap;
import com.jlboot.app.admin.base.BaseController;
import com.jlboot.core.cache.SysConfigCache;
import com.jlboot.core.form.FormData;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2020/6/1.
 */
public class SysConfigController extends BaseController {

    @JlActivity( name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    @JlActivity( name = "默认", log = false)
    public void SysConfigList() {
        FormData fd = FormData.n();
        setAttr("form", fd);
        render("SysConfigList.html");
    }

    /**
     * 列表数据提供API
     */
    @JlActivity( name = "默认", log = false)
    public void SysConfigListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_config").orderby(getRequest(), "sort");
        Page<SysConfig> pages = ps.paginate(SysConfig.dao);
        List<SysConfig> datalist = pages.getList();
        for (SysConfig st : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加
     */
    @JlActivity( name = "添加系统配置页面", log = false)
    public void AddSysConfig() {
        FormData fd = FormData.n().setActions("ADD");
        setAttr("cf", new SysConfig());
        setAttr("form", fd);
        render("ModifySysConfig.html");
    }

    /**
     * 修改页面
     */
    @JlActivity( name = "修改系统配置页面", log = false)
    public void EditSysConfig() {
        String id = getPara("id");
        SysConfig o = SysConfig.dao.findById(id);
        FormData fd = FormData.n().setActions("EDIT");
        if (o != null) {
            int cf_type = o.getInt("cf_type");
            String cf_value = o.getStr("cf_value");
            String cf_source = o.getStr("cf_source");
            if (cf_type == 9) {//选择框
                fd.addData("cfvalueHtml", SysConfigCache.getSelectHtml(cf_source, cf_value));
            } else {
                fd.addData("cfvalueHtml", SysConfigCache.getInputHtml(cf_value));
            }
        }
        setAttr("form", fd);
        setAttr("cf", o);
        render("ModifySysConfig.html");
    }

    /**
     * 修改API
     */
    @JlActivity( name = "修改系统配置API", log = false)
    public void ModifySysConfigApi() {
        //取得数据
        DataMap data = getDatas(getPara("postData"));
        String id = data.getStr("id");
        String cf_key = data.getStr("cf_key");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysConfig.dao.findById(id)
                    .set("cf_value", data.getStr("cf_value"))
                    .set("cf_desc", data.getStr("cf_desc"))
                    .update();
            SysConfigCache.reload();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysConfig()
                    .set("cf_key", data.getStr("cf_key"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改field API
     */
    @JlActivity( name = "修改系统配置field API", log = false)
    public void ModifySysConfigFieldApi() {
        String id = getPara("id");
        String field = getPara("field");
        String value = getPara("values");
        SysConfig.dao.findById(id).set(field, value).update();
        renderJson(JsonResult.okWithMsg("修改成功"));
    }

    /**
     * 清除cache
     */
    @JlActivity( name = "清除系统配置cacheAPI", log = false)
    public void ReloadSysConfigApi() {
        SysConfigCache.reload();
        renderJson(JsonResult.okWithMsg("清除成功"));
    }


    /**
     * 删除系统配置接口API
     */
    @JlActivity( name = "删除系统配置接口API", log = false)
    public void DelSysConfigApi() {
        String id = getPara("id");
        SysConfig demo = SysConfig.dao.findById(id);
        if (demo == null) {
            renderJson(JsonResult.fail( "记录不存在"));
        } else if (demo.delete()) {
            renderJson(JsonResult.okWithMsg("删除成功"));
        } else {
            renderJson(JsonResult.fail( "删除失败"));
        }
        ///////////////////////////////////
//
//        int cf_key = getParaToInt("cf_key", 0);
//        int counts = Db.queryInt("select count(*) from TABLENAME where cf_key=?", cf_key);
//        if (counts == 0) {
//            if (SysConfig.dao.findById(cf_key).delete()) {
//                renderJson(JsonResult.getResult(0, "删除成功"));
//            } else {
//                renderJson(JsonResult.getResult(9, "删除失败"));
//            }
//        } else {
//            renderJson(JsonResult.getResult(9, "该记录下有" + counts + "子记录,不能删除"));
//        }
    }

}

